package com.maple.service.impl;

import com.maple.entity.AddressBook;
import com.maple.enumeration.ExceptionEnum;
import com.maple.exception.BusinessException;
import com.maple.mapper.AddressBookMapper;
import com.maple.service.AddressBookService;
import com.maple.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
//        封装对象
//        TODO 前后端连桥时，要解开
//        addressBook.setUserId(ThreadLocalUtils.getCurrentId());
        addressBook.setIsDefault(0);

//        数据库新增
        addressBookMapper.add(addressBook);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
//        获得用户id
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());

//        数据库查询
        List<AddressBook> list = addressBookMapper.query(addressBook);

//        返回值
        return list;
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook queryById(String id) {
//        封装数据
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());
        addressBook.setId(Long.valueOf(id));

//        数据库查询
        List<AddressBook> list = addressBookMapper.query(addressBook);

//        返回值
        return list.get(0);
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook queryDefault() {
//        封装数据
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());
        addressBook.setIsDefault(1);

//        数据库查询
        List<AddressBook> list = addressBookMapper.query(addressBook);

        if(list == null || list.isEmpty()){
            throw new BusinessException(ExceptionEnum.NO_DEFAULT_ADDRESS_BOOK);
        }

//        返回值
        return list.get(0);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
//        封装数据——用户id
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());

//        数据库更新
        addressBookMapper.update(addressBook);

    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void updateDefault(AddressBook addressBook) {
//        封装数据
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setUserId(ThreadLocalUtils.getCurrentId());
        addressBook1.setIsDefault(0);

//      修改对应用户的所有地址的默认值
        addressBookMapper.update(addressBook1);

//        设置对应地址id的地址的默认值
//        addressBook.setId(Long.valueOf(id));
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);

    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deleteById(String id) {
//        数据库删除
        addressBookMapper.delete(Long.valueOf(id));

    }


}
